package com.mobileserver.dao;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.mobileserver.domain.Book;
import com.mobileserver.domain.BookClass;
import com.mobileserver.util.DB;

public class BookDAO {

	public List<Book> QueryBook(String barcode, String bookName,
			int bookClassId, Timestamp publishDate) {
		List<Book> bookList = new ArrayList<Book>();

		DB db = new DB();
		String sql = "select * from Book where 1=1";
		if (!barcode.equals(""))
			sql += " and barcode like '%" + barcode + "%'";
		if (!bookName.equals(""))
			sql += " and bookName like '%" + bookName + "%'";
		if (bookClassId != 0)
			sql += " and bookClassId=" + bookClassId;
		if(publishDate!=null)
			sql += " and publishDate='" + publishDate + "'";

		try {
			ResultSet rs = db.executeQuery(sql);
			while (rs.next()) {
				Book book = new Book();
				book.setBarcode(rs.getString("barcode"));
				book.setBookClassId(rs.getInt("bookClassId"));
				book.setBookImage(rs.getString("bookImage"));
				book.setBookName(rs.getString("bookName"));
				book.setCount(rs.getInt("count"));
				book.setPrice(rs.getFloat("price"));
				book.setPublishDate(rs.getTimestamp("publishDate"));
				bookList.add(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.all_close();
		}

		return bookList;

	}

	/* 传入图书对象，进行图书的添加业务 */
	public String AddBook(Book book) {
		DB db = new DB();
		String result = "";
		try {
			/* 如果所有验证通过就执行sql插入新商品信息 */
			String sqlString = "insert into Book(barcode,bookName,bookClassId,price,count,publishDate,bookImage) values ('";
			sqlString += book.getBarcode() + "','";
			sqlString += book.getBookName() + "',";
			sqlString += book.getBookClassId() + ",";
			sqlString += book.getPrice() + ",";
			sqlString += book.getCount() + ",'";
			sqlString += book.getPublishDate() + "','";
			sqlString += book.getBookImage() + "')";
			db.executeUpdate(sqlString);
			result = "图书添加成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "图书添加失败";
		} finally {
			db.all_close();
		}
		return result;
	}

	/* 删除图书 */
	public String DeleteBook(String barcode) {
		DB db = new DB();
		String result = "";
		try {
			String sqlString = "delete from Book where barcode='" + barcode
					+ "'";

			db.executeUpdate(sqlString);
			result = "图书删除成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "图书删除失败";
		} finally {
			db.all_close();
		}
		return result;
	}

	/* 根据条形码获取到图书信息 */
	public Book GetBook(String barcode) {
		Book book = null;
		DB db = new DB();
		String sql = "select * from Book where barcode='" + barcode + "'";
		try {
			ResultSet rs = db.executeQuery(sql);
			if (rs.next()) {
				book = new Book();
				book.setBarcode(rs.getString("barcode"));
				book.setBookClassId(rs.getInt("bookClassId"));
				book.setBookImage(rs.getString("bookImage"));
				book.setBookName(rs.getString("bookName"));
				book.setCount(rs.getInt("count"));
				book.setPrice(rs.getFloat("price"));
				book.setPublishDate(rs.getTimestamp("publishDate"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.all_close();
		}
		return book;
	}

	/* 更新图书信息 */
	public String UpdateBook(Book book) {
		DB db = new DB();
		String result = "";
		try {
			String sql = "update Book set bookName='";
			sql += book.getBookName() + "',bookClassId=";
			sql += book.getBookClassId() + ",price=";
			sql += book.getPrice() + ",count=";
			sql += book.getCount() + ",publishDate='";
			sql += book.getPublishDate() + "',bookImage='";
			sql += book.getBookImage() + "' where barcode='"
					+ book.getBarcode() + "'";
			db.executeUpdate(sql);
			result = "图书更新成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "图书更新失败";
		} finally {
			db.all_close();
		}
		return result;
	}
}
